package org.example.yiyu.service.impl;

import lombok.RequiredArgsConstructor;
import org.example.yiyu.mapper.LoginMapper;
import org.example.yiyu.model.User;
import org.example.yiyu.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {

    private final LoginMapper loginMapper;
    @Override
    public User login(String phoneNumber, String password) {

        User user = loginMapper.findByPhoneNumber(phoneNumber);

        if (user == null) {
            // 用户不存在，注册新用户
            User newUser = User.builder()
                    .phoneNumber(phoneNumber)
                    .password(password)
                    .build();
            loginMapper.insert(newUser);
            return newUser;
        }
        //检验密码是否正确
        if (password.equals(user.getPassword())) {
            return user;
        } else {
            return new User();
        }
    }

}
